home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 037a / tc256d.zip / BGIMOUSE.C < prev    next >
C/C++ Source or Header  |  1991-02-19  |  8KB  |  296 lines

  1. /*****************************************************************************
  2.    bgimouse.c  -  a quick (and maybe dirty) program that attaches a mouse
  3.                handler to the Microsoft mouse driver to provide hands-free
  4.                (interrupt driven) graphics cursor movement.
  5.  
  6.                It should work with all BGI drivers, in all graphics modes.
  7.                It works better (less flicker) with our 16 Super VGA drivers
  8.                since our getimage/putimage routines are faster.
  9.  
  10.                It is useful when using extended VGA modes (as with our
  11.                Super VGA BGI drivers) where the Microsoft graphics mouse
  12.                cursor CANNOT be used.
  13.  
  14.    Compile it as-is to see it work with Borland's BGI drivers
  15.           ...or if you have a fancy VGA card...
  16.    See comments in main() on how to use our BGI driver with this program.
  17.  
  18. Written by:
  19. Reagan Thomas  CIS: 73520,2067
  20.  
  21. Thomas Design
  22. P.O. Box 586
  23. Stillwater, OK 74076
  24.  
  25.  
  26. NOTE:  1) When using the mouse handler, it's a always good idea to say
  27.         HideMouse = TRUE;
  28.         when doing a lot of graphics drawing.
  29.  
  30. *****************************************************************************/
  31.  
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <stdarg.h>
  36. #include <graphics.h>
  37. #include <conio.h>
  38. #include <dos.h>
  39.  
  40. #include "isvgadet.h"
  41. #include "isvga256.h"
  42. #include "vgaextra.h"
  43.  
  44. /* defines for mouse events */
  45. #define  MOVED        0x01
  46. #define  L_PRESS      0x02
  47. #define  L_RELEASE    0x04
  48. #define  R_PRESS      0x08
  49. #define  R_RELEASE    0x10
  50. #define  M_PRESS      0x20
  51. #define  M_RELEASE    0x40
  52.  
  53. #define    TRUE        1
  54. #define    FALSE    0
  55.  
  56. union REGS inreg, outreg;
  57.  
  58. int    GraphDriver, GraphMode;
  59. int    MaxX, MaxY, ssize;
  60. int    ErrorCode;
  61. void *ptr, *screen;
  62. unsigned MouseBtn, MouseX, MouseY, OldMX, OldMY, OldButt;
  63. unsigned char MouseEvent, HideMouse = 0;
  64. int  XMofs = 0, YMofs = 0, OldxO, OldyO;
  65.  
  66.  
  67. /* -------------------------------------------------------------------------
  68.    This is the important routine.  Once it's hooked into the Microsoft mouse
  69.    driver, it keeps mouse variable/events up to date and updates the position
  70.    of the pointer icon
  71.    -------------------------------------------------------------------------*/
  72.  
  73. void interrupt gfxmousehandler(void) {
  74. unsigned int a, b, c, d;
  75.  
  76.  a = _AX;            /* save mouse status from uSoft mouse driver */
  77.  b = _BX;
  78.  c = _CX;
  79.  d = _DX;
  80.  
  81.  OldMX = MouseX;     /* store old mouse status */
  82.  OldMY = MouseY;
  83.  OldButt = MouseBtn;
  84.  MouseEvent |= a;    /* store new mouse status */
  85.  MouseBtn = b;
  86.  MouseX = c;
  87.  MouseY = d;
  88.  
  89.  if(!HideMouse) {
  90.     if((MouseX != OldxO) || (MouseY != OldyO)) {
  91.  
  92.  
  93.  /* Here I was experimenting with Vertical Sync in VGA modes...doesn't make
  94.     much difference and it cause problems (this is an interrupt, you know!)
  95.     Use it with caution.
  96.  
  97.     while(inport(0x3da) & 0x08);        wait til NOT in retrace
  98.     while(!(inport(0x3da) & 0x08));    wait til very start of retrace  */
  99.  
  100.  
  101.  /* update pointer icon position */
  102.  
  103.         putimage(OldxO, OldyO, screen, COPY_PUT);
  104.         getimage(MouseX, MouseY, MouseX+XMofs, MouseY+YMofs, screen);
  105.         putimage(MouseX, MouseY, ptr, OR_PUT);
  106.         OldxO = MouseX;    OldyO = MouseY;
  107.     }
  108.  }
  109.                     /* Recover registers and return to mouse driver */
  110.  __emit__(
  111.         0x8b, 0x0e5,   /*    MOV    SP,BP */
  112.         0x5d,          /*   POP  BP    */
  113.         0x07,          /*   POP  ES    */
  114.         0x1f,          /*   POP  DS    */
  115.         0x5f,          /*   POP  DI    */
  116.         0x5e,          /*   POP  SI    */
  117.         0x5a,          /*   POP  DX    */
  118.         0x59,          /*   POP  CX    */
  119.         0x5b,          /*   POP  BX    */
  120.         0x58,          /*   POP  AX    */
  121.         0x0cb          /*   RETF       */
  122.         );
  123. }
  124.  
  125.  
  126. /* initializes the mouse driver */
  127.  
  128. int init_mouse(void) {
  129. int    test;
  130.  
  131.  _AX = 0;
  132.  geninterrupt(51);
  133.  test = _AX;
  134.  if(test == 0)
  135.     return(FALSE);
  136.  else
  137.     return(TRUE);
  138. }
  139.  
  140. /* Enables the text mode mouse cursor */
  141. void mouse_cursor(void) {
  142.  _AX = 1;
  143.  geninterrupt(51);
  144. }
  145.  
  146. /* Disables the text mode mouse cursor */
  147. void disable_cursor(void) {
  148.  _AX = 2;
  149.  geninterrupt(51);
  150. }
  151.  
  152. /* Returns the current status of the mouse */
  153. void read_mouse(int *mousex, int *mousey, int *button) {
  154.  _AX = 3;
  155.  geninterrupt(51);
  156.  *button = 3&_BX;
  157.  *mousex = _CX;
  158.  *mousey = _DX;
  159. }
  160.  
  161. /* Sets the mouse XY counts */
  162. void move_mouse(int mousex, int mousey) {
  163.  _AX = 4;
  164.  _CX = mousex;
  165.  _DX = mousey;
  166.  geninterrupt(51);
  167. }
  168.  
  169. /* Sets the mouse speed */
  170. void mouse_speed(int speed) {
  171.  _AX = 0x0013;
  172.  _DX = speed;
  173.  geninterrupt(51);
  174. }
  175.  
  176. /* Set limits for mouse counts */
  177. void mouse_range(int xmin, int ymin, int xmax, int ymax) {
  178.  _AX = 7;
  179.  _CX = xmin;
  180.  _DX = xmax;
  181.  geninterrupt(51);
  182.  _AX = 8;
  183.  _CX = ymin;
  184.  _DX = ymax;
  185.  geninterrupt(51);
  186. }
  187.  
  188. /* Sets the ratio of X to Y mouse counts */
  189. void mouse_ratio(int xaxis, int yaxis) {
  190.  _AX = 0x000f;
  191.  _CX = xaxis;
  192.  _DX = yaxis;
  193.  geninterrupt(51);
  194. }
  195.  
  196. /* Installs the mouse handler by hooking into the Microsoft driver */
  197.  
  198. void InstallMouseHandler(unsigned mask, unsigned taskSeg, unsigned taskOfs) {
  199. struct SREGS seg;
  200.  
  201.  inreg.x.ax = 12;
  202.  inreg.x.cx = mask;
  203.  inreg.x.dx = taskOfs;
  204.  seg.es = taskSeg;
  205.  int86x(0x33, &inreg, &outreg, &seg);
  206.  }
  207.  
  208.  
  209.  
  210. void main(void) {
  211.  
  212.  if((init_mouse()) == 0) {
  213.     printf("\nMOUSE DRIVER not active!!!\n\n\n\n\n");
  214.     exit(1);
  215.  }
  216.  
  217.  /* If you would like to try our Super VGA 16 color integrated driver...
  218.     ...un-comment-out the following line.--
  219.                                    |
  220.   -----------------------------------------
  221.  |
  222.  |  NOTE: This driver (ISVGA256.BGI)
  223.  |  supports 256 color modes from 640x400 to 1024x786 on the following VGA
  224.  |  cards: ATI, Techmar, Video7 VRAM, Paradise, Orchid, cards using the
  225.  |  Paradise chipset, the Tseng Labs chipset, the Trident chipset, and those
  226.  |  using the Chips and Tech chipset.
  227.  |  The first five cards listed will autodetect(), but for the others, you
  228.  |  may have the 'force' the driver to be active.  To force the driver, replace
  229.  |  the line:
  230.  |
  231.  |  GraphDriver = DETECT;
  232.  |       with
  233.  |  GraphDriver = 134;
  234.  |
  235.  \/                        */
  236.  
  237.  installuserdriver("ISVGA256",DetectISVGA256);
  238.  GraphDriver = DETECT;
  239.  initgraph(&GraphDriver, &GraphMode, "");
  240.  
  241.  ErrorCode = graphresult();
  242.  if(ErrorCode != grOk){
  243.    printf(" Graphics System Error: %s\n", grapherrormsg(ErrorCode));
  244.    exit(1);
  245.  }
  246.  
  247.  MaxX = getmaxx();
  248.  MaxY = getmaxy();                /* Read size of screen */
  249.  
  250.  mouse_speed(10);                  /* setup mouse related stuff */
  251.  mouse_ratio(8,15);
  252.  disable_cursor();
  253.  mouse_range(0, 0, MaxX, MaxY);
  254.  move_mouse(80,80);
  255.  
  256.  setcolor(255);
  257.  
  258.  setfillstyle(1,1);
  259.  bar(0,0,getmaxx(),getmaxy());
  260.                                 /* Draw the pointer */
  261.  moveto(26, 36);    lineto(26, 52);
  262.  lineto(30, 51);    lineto(31, 55);
  263.  lineto(36, 54);    lineto(34, 49);
  264.  lineto(37, 47);    lineto(26, 36);
  265.  
  266.  ssize  = imagesize(26, 36, 37, 55);    /* reserve space for getimage() */
  267.  ptr    = malloc(ssize);
  268.  screen = malloc(ssize);
  269.  
  270.  setfillstyle(1,4);
  271.  floodfill(29,40,255);
  272.  getimage(26, 36, 37, 55, ptr);         /* 'get' the pointer icon       */
  273.  getimage(100, 100, 111, 119, screen);  /* 'get' some blank screen...   */
  274.  putimage(26, 36, screen, COPY_PUT);    /* ...and cover up the icon     */
  275.  
  276.  XMofs = 11;    YMofs = 19; /* these vars specify icon size */
  277.  OldxO = 26;    OldyO = 36; /* tell my mouse handler where the icon was drawn */
  278.  
  279.  InstallMouseHandler(0x15, FP_SEG(gfxmousehandler), FP_OFF(gfxmousehandler));
  280. /*                  ^
  281.                   |__ setup events for movement and buttons released  */
  282.  
  283.  
  284. outtextxy(10,100, "Go ahead and move the mouse - press a key to quit");
  285.  
  286.  while(!kbhit());   /* loop til key pressed, mouse handler does the rest! */
  287.  while(kbhit())
  288.     getch();
  289.  
  290.  init_mouse();        /* MUST unhook gfx mouse handler before swapping to text...
  291.                 ...else really HORRIBLE things happen !!!             */
  292.  restorecrtmode();
  293.  
  294. }
  295.  
  296.